﻿package com.xuanque.mahjongDice.control
{
	import com.xuanque.mahjongDice.util.LoaderImg;
	
	import datas.Player;
	import com.xuanque.mahjongDice.control.GameAction;
	import com.xuanque.mahjongDice.util.Tools;
	
	import flash.display.MovieClip;
	import flash.utils.getDefinitionByName;
	import utils.HallConfig; //
	
	public class ImgAction
	{
		private var gameAreaControl:*;
		private var PhotoImg:Class;
		
		private static var Instance:ImgAction;
		
		public function ImgAction(_gameAreaControl:*)
		{
			Instance = this;
			gameAreaControl = _gameAreaControl;
		}
		
		/*
		 * 单件类的实例 
		*/
		public static function getInstance():ImgAction {
			
			return Instance;
		}
		
		
		public function setStaticObjectNull():void{
		    Instance = null;
		}
		
		private function initUser(player:Player):void {
			
			
			
			var curPos = GameAction.getInstance().getUserPos();
			var userPos;
			
			trace("player.getPosition() ===== " + player.getPosition() + " GameAction.getInstance().getUserPos() === " + curPos);
			
			if ( player.getPosition() == curPos ) {
				userPos = 0;
			}else{
				userPos = Tools.getPositionByPos(curPos, player.getPosition());
			}
			
			trace("转换后的位置 ===== " + userPos);
			
		    var testPlayer:MovieClip = gameAreaControl["player" + userPos];
			
			this.DisplayChildren(testPlayer, true); //显示 头像 和 用户名
			
			
			//testPlayer.player_txt.text = player.getLoginName();
			testPlayer.player_txt.text = player.getNickName();
			LoaderImg.getInstance().loadPhoto(testPlayer.photo_mc, player);
			
		}
		
		private function clearUser(player:Player):void {
			
		    var curPos = GameAction.getInstance().getUserPos();
		    var userPos = Tools.getPositionByPos(curPos, player.getPosition());
			
			var testPlayer:MovieClip = gameAreaControl["player" + userPos];
			
			testPlayer.player_txt.text = "";
			
			this.DisplayChildren(testPlayer, false);//隐藏 头像 和 用户名
			
		    while (testPlayer.photo_mc.numChildren > 1) {
				
				testPlayer.photo_mc.removeChildAt(testPlayer.photo_mc.numChildren-1);
		    }
			
			
		}
		
		/*
		 * 加入
		*/
		public function enterUser(player:Player):void{
			
		    initUser(player);
		}
		
		/*
		 * 退出
		*/
		public function leaveUser(player:Player):void{
		   
		   clearUser(player);
		}
		
		
		/*
		//强退
		public function outOfGame(player:Player):void{
		  outGame(player);
		}
		
		
		//断线
		private function outGame(player:Player):void{
			var effect:MovieClip = new EffectClass.OutServer();
		    var curPos = CoreAction.getInstance().getCurPosition();
		    var userPos = Tool.getPositionByPos(curPos,player.getPosition());
		    gameAreaControl["player"+userPos].userImg.addChild(effect);
		}
		
		//断线的人回来
		
		public function userComeBack(player:Player):void{
		  var curPos = CoreAction.getInstance().getCurPosition();
		  var userPos = Tool.getPositionByPos(curPos,player.getPosition());
		  while(gameAreaControl["player"+userPos].userImg.numChildren>2){
		      gameAreaControl["player"+userPos].userImg.removeChildAt(gameAreaControl["player"+userPos].userImg.numChildren-1);
		  }
		}
		
		*/
		
		
		
		
		
		/*
		 *  显示 和 隐藏 MovieClip
		*/
		private function DisplayChildren(testPlayer:*,isDisplay = true):void {
			
			testPlayer.player_txt.visible = isDisplay;
			testPlayer.photo_mc.visible = isDisplay;
			testPlayer.kuang_mc.visible = isDisplay;
			
		}
		
		
		
		
	}
}